package com.angel.web.health;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzCmsColumn;
import com.angel.health.BuzCmsContent;
import com.angel.health.BuzCmsContentWithBLOBs;
import com.angel.provider.health.BuzCmsColumnProvider;
import com.angel.provider.health.BuzCmsContentProvider;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年11月29日 上午10:35:04 类说明
 */
@Controller
@RequestMapping("cms")
public class BuzCmsController {

	@Autowired
	private BuzCmsColumnProvider bccProvider;
	
	@Autowired
	private BuzCmsContentProvider buzCmsContentProvider;

	/**
	 * 内容管理
	 * 
	 * @return
	 */
	@RequestMapping("content")
	public String content(BuzCmsContentWithBLOBs bccwb,HttpServletRequest req,Pager pager) {
		pager = buzCmsContentProvider.findAll(bccwb,pager);
		req.setAttribute("bean", pager);
		req.setAttribute("bcc", bccwb);
		return "cms/content";
	}

	/**
	 * 栏目管理
	 */
	@RequestMapping("column")
	public String column(HttpServletRequest req) {
		BuzCmsColumn bcc = new BuzCmsColumn();
		bcc.setEnable(1);
		List<Map<String, Object>> list = bccProvider.findAll(bcc);
		req.setAttribute("list", list);
		return "cms/column";
	}

	@ResponseBody
	@RequestMapping(value="index", produces = "application/json; charset=utf-8")
	public String index(BuzCmsColumn bcc) {
		List<Map<String, Object>> list = bccProvider.findAll(bcc);
		return JsonUtil.toJson(list);
	}

	/**
	 * 添加栏目
	 * @param bcc
	 * @return
	 */
	@ResponseBody
	@RequestMapping("saveColumn")
	public Integer saveColumn(BuzCmsColumn bcc,HttpServletRequest request) {
		String createBy = request.getSession().getAttribute("user").toString();
		bcc.setUpdateBy(createBy);
		bcc.setCreateBy(createBy);
		int insert = bccProvider.insert(bcc);
		return insert;
	}
	
	@ResponseBody
	@RequestMapping("saveContent")
	public Integer saveContent(BuzCmsContentWithBLOBs bccwb,HttpServletRequest request){
		String createBy = request.getSession().getAttribute("user").toString();
		bccwb.setUpdateBy(createBy);
		bccwb.setCreateBy(createBy);
		int insert = buzCmsContentProvider.insert(bccwb);
		return insert;
	}
	
	/**
	 * 修改栏目
	 * @param bcc
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editColumn")
	public Integer editColumn(BuzCmsColumn bcc){
		int update = bccProvider.updateById(bcc);
		return update;
	}
	
	@ResponseBody
	@RequestMapping("editContent")
	public Integer editContent(BuzCmsContentWithBLOBs bcc){
		int update = buzCmsContentProvider.updateById(bcc);
		return update;
	}
	
	
	
	@ResponseBody
	@RequestMapping("delColumn")
	public Integer delColumn(BuzCmsColumn bcc){
		int del = bccProvider.delById(bcc);
		return del;
	}
	
	@ResponseBody
	@RequestMapping("delContent")
	public Integer delContent(BuzCmsContentWithBLOBs bcc){
		int del = buzCmsContentProvider.delById(bcc);
		return del;
	}
	
	@ResponseBody
	@RequestMapping("findColumnById")
	public String findColumnById(BuzCmsColumn bcc){
		 List<Map<String, Object>> list = bccProvider.findAll(bcc);
		 if(list.size() > 0){
			 return JsonUtil.toJson(list.get(0));			 
		 }else{
			 return JsonUtil.toJson("");
		 }
	}
	
	@ResponseBody
	@RequestMapping("findContentById")
	public String findContentById(BuzCmsContentWithBLOBs bcc){
		 List<Map<String, Object>> list = buzCmsContentProvider.findAll(bcc);
		 if(list.size() > 0){
			 return JsonUtil.toJson(list.get(0));			 
		 }else{
			 return JsonUtil.toJson("");
		 }
	}

}
